#include "dialog.h"
#include "ui_dialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QDebug>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    _imgCopy_Thread = nullptr;
    ui->imageRadioBtn->setChecked(true);

}

Dialog::~Dialog()
{
    if(!_imgCopy_Thread){
        qDebug() << "线程释放完成";
    }
    delete ui;
}


void Dialog::on_srcFolderBtn_clicked()
{
    // 获取需要移动的跟文件夹目录
    // ShowDirsOnly 只显示目录，默认情况下显示文件和目录
    // DontResolveSymlinks 不解析链接【window中快捷方式】 不解析它真实的路径，就是原始路径
    // 而是当前它所在的路径，比如桌面快捷方式，真实路径可能在c盘或者d盘或者其他，不解析它的路径就是c盘
    QString folderPath = QFileDialog::getExistingDirectory(this,tr("请选择文件夹"),"."
                   ,QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);

    if(!folderPath.isEmpty()){
        ui->srcFolderPath->setText(folderPath);
    }

}

void Dialog::on_targetFolderBtn_clicked()
{
    // 获取目标路径
    QString folderPath = QFileDialog::getExistingDirectory(this,tr("请选择文件夹"),"."
                   ,QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    QString srcPath = ui->srcFolderPath->text();

    if(srcPath.compare(folderPath) == 0){
        QMessageBox::information(this,("提示"),tr("原始路径和目标路径相同，请选择不同路径下进行拷贝"));
        return;
    }

    if(!folderPath.isEmpty()){
        ui->targetFolderPath->setText(folderPath);
    }
}

void Dialog::on_startBtn_clicked()
{

    QStringList list = getCopyListType();
    if(list.isEmpty()){
        return;
    }



    QString srcPath = ui->srcFolderPath->text();
    QString targetPath = ui->targetFolderPath->text();

    QString strPath = QString("是否将文件从 '%1' 路径拷贝到 '%2' 路径下").arg(srcPath).arg(targetPath);
    QMessageBox::StandardButton sBtn = QMessageBox::information(this,"确认",strPath,QMessageBox::Ok,QMessageBox::Cancel);
    if(sBtn == QMessageBox::Cancel){
        return;
    }
    ui->progressBar->setValue(0);


    QString msg;
    if(srcPath.isEmpty()){
       msg += "原始路径为空";
    }
    if(targetPath.isEmpty()){
        msg += " 目标路径为空";
    }

    if(!msg.isEmpty()){
        QMessageBox::warning(this,tr("警告"),tr("%1%2").arg(msg).arg("请检查重试"));
        return;
    }



    // 创建线程
    _imgCopy_Thread = new ImageCopyThread(srcPath,targetPath);
    _imgCopy_Thread->isCutToNewPath(false);
    connect(_imgCopy_Thread,&ImageCopyThread::progressUpdate,
            this,[=](int progress,const QString& status){
        ui->progressBar->setValue(progress);
        ui->statusLabel->setText(status);
    });

    connect(_imgCopy_Thread,&ImageCopyThread::finished,_imgCopy_Thread,&ImageCopyThread::deleteLater);
    connect(_imgCopy_Thread,&ImageCopyThread::destroyed,[&](){
        _imgCopy_Thread = nullptr;
        ui->startBtn->setEnabled(true);
    });

    // 给线程设置类型
    _imgCopy_Thread->setCopyType(list);

    _imgCopy_Thread->start();

    ui->startBtn->setEnabled(false);

}

void Dialog::on_cutBtn_clicked()
{
    QStringList list = getCopyListType();
    if(list.isEmpty()){
        return;
    }

    QString srcPath = ui->srcFolderPath->text();
    QString targetPath = ui->targetFolderPath->text();

    QString strPath = QString("是否将文件从 '%1' 路径剪切到 '%2' 路径下").arg(srcPath).arg(targetPath);
    QMessageBox::StandardButton sBtn = QMessageBox::information(this,"确认",strPath,QMessageBox::Ok,QMessageBox::Cancel);
    if(sBtn == QMessageBox::Cancel){
        return;
    }
    ui->progressBar->setValue(0);


    QString msg;
    if(srcPath.isEmpty()){
       msg += "原始路径为空";
    }
    if(targetPath.isEmpty()){
        msg += " 目标路径为空";
    }

    if(!msg.isEmpty()){
        QMessageBox::warning(this,tr("警告"),tr("%1%2").arg(msg).arg("请检查重试"));
        return;
    }

    // 创建线程
    _imgCopy_Thread = new ImageCopyThread(srcPath,targetPath);
    _imgCopy_Thread->isCutToNewPath(true);

    connect(_imgCopy_Thread,&ImageCopyThread::progressUpdate,
            this,[=](int progress,const QString& status){
        ui->progressBar->setValue(progress);
        ui->statusLabel->setText(status);
    });

    connect(_imgCopy_Thread,&ImageCopyThread::finished,_imgCopy_Thread,&ImageCopyThread::deleteLater);
    connect(_imgCopy_Thread,&ImageCopyThread::destroyed,[&](){
        _imgCopy_Thread = nullptr;
        ui->cutBtn->setEnabled(true);
    });

    // 给线程设置类型
    _imgCopy_Thread->setCopyType(list);

    _imgCopy_Thread->start();

    ui->cutBtn->setEnabled(false);
}

QStringList Dialog::getCopyListType()
{
    QStringList list;
    if(ui->imageRadioBtn->isChecked()){
        // 图片按键选中
        list = QStringList({"jpg","jpeg","png","bmp","gif"});
    }else if(ui->customRadioBtn->isChecked()){
        QString typeStr = ui->typeListLineedit->text();
        if(!typeStr.isEmpty()){
            list = typeStr.split(";");
        }else{
            QMessageBox::warning(this,tr("警告"),tr("类型输入为空,请检查重试"));
            return QStringList();
        }
    }else{
        return QStringList();
    }

    return list;
}
